using System;
using System.ComponentModel;
using System.Windows.Media.Imaging;
using System.Windows.Threading;
using Microsoft.WindowsAPICodePack.Shell;

namespace TouchExplorer
{
    public abstract class TouchFileElementBase : INotifyPropertyChanged, IDisposable
    {
        private BitmapSource _image;

        private BitmapSource _bigimage;

        public string FullPath { get; protected set; }

        public string Name { get; protected set; }

        public ShellObject Shell { get; set; }

        public BitmapSource Image
        {
            get
            {
                if (Shell == null)
                {
                    Shell = ShellObject.FromParsingName(FullPath);
                }
                if (_image == null) 
                    _image = Shell.Thumbnail.MediumBitmapSource;
                return this._image;
            }
            set
            {
                this._image = value;
                this.OnPropertyChanged("Image");
            }
        }

        public BitmapSource BigImage
        {
            get
            {
                if (Shell == null)
                {
                    Shell = ShellObject.FromParsingName(FullPath);
                }
                if (_bigimage == null)
                    _bigimage = Shell.Thumbnail.ExtraLargeBitmapSource;
                return this._bigimage;
            }
            set
            {
                this._bigimage = value;
                this.OnPropertyChanged("BigImage");
            }
        }

        private bool _selected;

        public bool IsSelected
        {
            get
            {
                return this._selected;
            }
            set
            {
                this._selected = value;
                this.OnPropertyChanged("IsSelected");
            }
        }

        private bool _copyed;

        public bool IsCopyed
        {
            get
            {
                return this._copyed;
            }
            set
            {
                this._copyed = value;
                this.OnPropertyChanged("IsCopyed");
            }
        }

        private bool _cutted;

        public bool IsCutted
        {
            get
            {
                return this._cutted;
            }
            set
            {
                this._cutted = value;
                this.OnPropertyChanged("IsCutted");
            }
        }

        private bool _favorite;

        public bool IsFavorite
        {
            get
            {
                return this._favorite;
            }
            set
            {
                this._favorite = value;
                this.OnPropertyChanged("IsFavorite");
            }
        }


        public virtual event PropertyChangedEventHandler PropertyChanged;
        
        ~TouchFileElementBase()
        {
            this.Dispose(false);
            GC.SuppressFinalize(this);
        }

        public override bool Equals(object obj)
        {
            if (!(obj is TouchFileElementBase)) return false;

            return (obj as TouchFileElementBase).FullPath == FullPath;
        }

        public override string ToString()
        {
            return Name;
        }

        protected void OnPropertyChanged(string p)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(p));
        }

        public void Dispose()
        {
            Dispose(true);
        }

        protected virtual void Dispose(bool disposing)
        {
            if (Shell != null)
                Shell.Dispose();
            Shell = null;
            BigImage = null;
            Image = null;
        }

        public abstract bool Exists { get; }
    }
}